package com.lh_travel.lohas.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RadioButton;

import com.lh_travel.lohas.R;
import com.lh_travel.lohas.adapter.CommonAdapter;
import com.lh_travel.lohas.adapter.ViewHolder;
import com.lh_travel.lohas.base.baseActivity;
import com.lh_travel.lohas.domain.Comment;
import com.lh_travel.lohas.domain.DafultMessageEvent;
import com.lh_travel.lohas.domain.HotelFilterBean;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.util.ArrayList;

public class PositionFilterActivity extends baseActivity {

    private ImageButton img_back;
    private RadioButton rb_station, rb_recomend;
    private RadioButton rb_view;
    private ListView lv_item;
    private ArrayList<Comment> scenery;
    private ArrayList<Comment> station;
    private ArrayList<Comment> dataList;
    private CommonAdapter adapter;
    private String checkedItemPosition;
    private HotelFilterBean filterBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_position_filter);

        findView();
        bindListner();
        ensureUI();
    }

    @Override
    public void findView() {

        img_back = (ImageButton) findViewById(R.id.img_back);
        rb_station = (RadioButton) findViewById(R.id.rb_station);
        rb_view = (RadioButton) findViewById(R.id.rb_view);
        rb_recomend = (RadioButton) findViewById(R.id.rb_recomend);
        lv_item = (ListView) findViewById(R.id.lv_item);

        rb_recomend.setChecked(true);
    }

    @Override
    public void bindListner() {

        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        rb_recomend.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (isChecked) {

                    checkedItemPosition = "";

                    dataList.clear();
                    dataList.addAll(filterBean.position_hot);
                    adapter.notifyDataSetChanged();
                }
            }
        });

        rb_station.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (isChecked) {

                    checkedItemPosition = "";

                    dataList.clear();
                    dataList.addAll(filterBean.position_station);
                    adapter.notifyDataSetChanged();
                }
            }
        });

        rb_view.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (isChecked) {

                    checkedItemPosition = "";

                    dataList.clear();
                    dataList.addAll(filterBean.position_scenery);
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    public void ensureUI() {

        filterBean = (HotelFilterBean) getIntent().getSerializableExtra("detail");

        initLV();
    }

    private void initLV() {

        dataList = new ArrayList<>();
        dataList.addAll(filterBean.position_hot);

        adapter = new CommonAdapter<Comment>(mContext, dataList, R.layout.item_position_filter) {
            @Override
            public void convert(ViewHolder helper, Comment item, int position) {

                helper.setText(R.id.tv_title, item.title);
                helper.setImageUrl(R.id.img_pic, item.picture);

                if (checkedItemPosition != null && checkedItemPosition.equals(position +"")){

                    helper.setViewCheck(R.id.tv_title, true);

                }else {
                    helper.setViewCheck(R.id.tv_title, false);
                }
            }
        };

        lv_item.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);

        lv_item.setAdapter(adapter);

        lv_item.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                checkedItemPosition = dataList.get(lv_item.getCheckedItemPosition()).title;

                adapter.notifyDataSetChanged();

                DafultMessageEvent dafultMessageEvent = new DafultMessageEvent();

                dafultMessageEvent.TAG = "PositionFilterActivity";
                dafultMessageEvent.message = checkedItemPosition;
                dafultMessageEvent.belong = dataList.get(lv_item.getCheckedItemPosition()).id;

                EventBus.getDefault().post(dafultMessageEvent);

                finish();
            }
        });
    }


}
